﻿<script>

    var _shops = @this.RawSerialize(this.ViewBag.ListShop); 
    var _sts = [{ "ID": 0, "Title": "待付款" }, { "ID": 1, "Title": "已付款，配货中" }, { "ID": 2, "Title": "已发货" }, { "ID": 3, "Title": "已收货" }, { "ID": 4, "Title": "订单结束" }, { "ID": 10, "Title": "取消订单" }];     var _tps = [{ "ID": -1, "Title": "全部订单" },{ "ID": 0, "Title": "会员专享" }, { "ID": 1, "Title": "商城订单" }]; 
    var _refounds = [{ "ID": "SUCCESS", "Title": "退款成功" }, { "ID": "REFUNDCLOSE", "Title": "退款关闭" }, { "ID": "PROCESSING", "Title": "退款处理中" }, { "ID": "CHANGE", "Title": "退款异常" },];

    
     

    var _vm;
    $(function () {
        var vm = new PageViewModel();
        _vm = vm;
        ko.applyBindings(vm);
        vm.init();


    });



    function PageViewModel() {
        var me = this;
        ViewModel.call(me);
        vmExtend.call(me);

        me.url = "/Wiki/ShopOrder/GetModels";
        me.deleteUrl = "/Wiki/ShopOrder/Delete";
        me.dialog = new FormDialog(me);


        me.search2 = function () {
            me.loadData2();
        }

        me.loadData2 = function (page) {
            if (me.url == null)
                return;

            var data = me.getSearchModel();
            if (page)
                data.page = page;
            $ace.get("/Wiki/ShopOrder/Export", data, function (result)
            {
                console.log(result.Data);
                window.location.href = result.Data;
            });
        }

         
     }
    function vmExtend() {
        var me = this;
    }

    function FormDialog(vm) {
        var me = this;
        Dialog.call(me);

        me.onOpen = function () {
            var model = me.editModel();
            if (model)
            {
                var bindModel = $ko.toJS(model);
                me.model(bindModel);
                console.log(bindModel);

                $ace.get("/API/GetUsersChargeLeft", { UserID: bindModel.CreateID }, function (result)
                {
                    jQuery("#UserCharge").val(result.Data);
                });
                

            }
            else {
                me.model({ IsEnabled: true });
            }
        }

        me.onSave = function () {
            if (!$('#form1').formValid()) {
                return false;
            }

            

            var model = me.model();

            var UserCharge = jQuery("#UserCharge").val();
            if (parseFloat(UserCharge) < 0)
            {
                if (model.ST == 2 || model.ST == 3) {
                    $ace.msg("订单金额<0时，无法安排发货。");
                    return;
                }
                
            }

            if (me.editModel()) {
                $ace.post("/Wiki/ShopOrder/Update", model, function (result) {
                    $ace.msg(result.Msg);
                    me.close();
                    vm.dataTable.reload();
                });
            }
            else {
                $ace.post("/Wiki/ShopOrder/Add", model, function (result) {
                    $ace.msg(result.Msg);
                    me.close();
                    vm.loadData();
                }
                );
            }
        }
    }
     
</script>
